package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.service.ReportAgeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/reportAge")
public class ReportAgeController {
    @Reference
    private ReportAgeService reportAgeService;

    @RequestMapping("/action")
    public Result action() {
        Map<String, Object> map = new HashMap<>();

        List<String> ages = new ArrayList<>();
        ages.add("婴幼儿");
        ages.add("少儿");
        ages.add("青少年");
        ages.add("青年");
        ages.add("中年");
        ages.add("老年");

        List<String> list = getDateList();
        List<Integer> numbers = reportAgeService.findNumbersByDateList(list);

        map.put("ages", ages);
        map.put("numbers", numbers);
        return new Result(true, "上传会员年龄分布图成功", map);
    }

    public List<String> getDateList() {
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();

        String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        calendar.add(Calendar.YEAR, -6);
        String sixYearsAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        calendar.add(Calendar.YEAR, -6);
        String twelveYearsAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        calendar.add(Calendar.YEAR, -6);
        String eighteenYearsAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        calendar.add(Calendar.YEAR, -22);
        String fortyYearsAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        calendar.add(Calendar.YEAR, -20);
        String sixtyYearsAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        calendar.add(Calendar.YEAR, -140);
        String twoHundredYearsAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        list.add(nowTime);
        list.add(sixYearsAgo);
        list.add(twelveYearsAgo);
        list.add(eighteenYearsAgo);
        list.add(fortyYearsAgo);
        list.add(sixtyYearsAgo);
        list.add(twoHundredYearsAgo);

        return list;
    }
}
